{include file="admin@public/header" /}
<link rel="stylesheet" href="/public/static/admin/css/report.css" />
<link rel="stylesheet" href="/public/static/admin/easyui/layui-v2.6.8/css/layui.css">


<link href="/public/static/admin/blue/bills.css?ver=20210803202401" rel="stylesheet" type="text/css">
<style>
  .wrapper {
    padding: 0;
  }

  .mod-search {
    padding: 5px 0 5px 16px;
  }

  .gridArea {
    padding: 7px 14px 0 16px;
  }

  .gridArea .btns {
    padding: 0 0 7px 0;
  }

  .ui-jqgrid-bdiv tr.ui-state-highlight {
    background: #f8ff94
  }

  .con-footer ul li label.wt55 {
    /* width: 55px; */
    width: auto;
  }

  #billTitle.wt190 {
    width: 320px;
  }

  .con-footer ul.maxwt-none {
    max-width: none;
  }

  .ui-jqgrid a.xhtRed {
    color: red;
  }

  .ui-jqgrid a.xhtRed:hover {
    color: red;
  }

  .con-footer .ctn-wrap {
    display: inline;
    padding: 0 6px 0 5px;
  }

  .con-footer ul li {
    min-width: 185px;
  }

  #billNo {
    width: 293px;
  }

  .layui-form-checkbox[lay-skin=primary]:hover i {
    border-color: #5dc5e7;
  }

  .layui-form-checked[lay-skin=primary] i {
    background-color: #5dc5e7;
  }

  .layui-form-radio {
    line-height: 30px;
  }

  .layui-form-radio *,
  .layui-form-checkbox span {
    font-size: 12px;
  }

  .con-footer ul li label {
    width: 66px;
  }

  .layui-form-radio {
    margin: 0;
  }

  .layui-form-radio>i:hover,
  .layui-form-radioed>i {
    border-color: #5dc5e7;
  }

  .layui-form-radio>i:hover,
  .layui-form-radioed>i {
    color: #5dc5e7;
  }

  .layui-form-checkbox[lay-skin=primary] i {
    padding: 0;
  }

  #filterItems {
    margin-right: 0;
  }
</style>
</head>


<div class="wrapper">

  <!-- header -->
  <div class="mod-search cf" id="report-search">
    <div class="l" id="filter-menu">
      <ul class="ul-inline fix layui-form" id="filterItems" style="height: auto;font-size: 12px;">
        <li id="date" style="display: list-item;">
          <label>单据日期:</label>
          <input type="date" id="startdate" value="" class="ui-input " name="filter-fromDate" id="filter-fromDate"
            maxlength="10" style="width: 120px;">
          <span class="todate"> 至 </span>
          <input type="date" id="enddate" value="" class="ui-input " name="filter-toDate" id="filter-toDate"
            maxlength="10" style="width: 120px;">
        </li>
        <li id="billsNo" style="display: list-item;">
          <label>单据编号:</label>
          <input type="text" class="ui-input ui-input-ph" id="billsNoAuto">
        </li>
        <li id="customer" style="display: list-item;">
          <label>客户:</label>
          <span class="mod-choose-input" id="filter-customer">
            <input type="text" class="ui-input ui-combo-active" id="customerAuto" autocomplete="off">
            <span class="ui-icon-ellipsis" onclick="chooseSupplier();"></span>
          </span>
        </li>
        <li id="billType" style="display: list-item;">
          <input type="radio" name="billType" value="0" title="已全部开票">
          <input type="radio" name="billType" value="1" title="未全部开票" checked="">
        </li>
        <li id="billType" style="display: list-item;">
          <input type="checkbox" name="showSubtotal" id="" lay-skin="primary" title="税额为0不显示" value="1">
          <div class="btns" style="top: -2px;float: right;position: relative;margin-top: 0;margin-left: 10px;"><a
              class="ui-btn mrb ui-btn-search" id="filter-submit">查询</a></div>
        </li>
      </ul>
    </div>
  </div>

  <!-- 功能区 -->
  <div class="gridArea" >
    <!-- 数据列表 -->
    <div class="cf btns">
      <div class="fr">
        <a id="save" onclick="savedata();" class="ui-btn-bill ui-btn-add btn-sm fl mr10">确定</a>
        <a href="#" class="ui-btn-bill ui-btn-hl btn-sm fl mr10" id="billRecord" onclick="addPage('/sales/salesbilling/index.html','sales','销售开票记录')">开票记录</a>
        <a href="#" class="ui-btn-bill ui-btn-hl btn-sm fl" id="export">导出</a>
      </div>
    </div>
    <div class="grid-wrap">
      <table id="grid">
      </table>
      <div id="page"></div>
    </div>
    <!-- 底部信息栏 -->
    <div class="con-footer cf">
      <ul id="amountArea" class="cf maxwt-none">
        <li>
          <label class="wt55">开票日期:</label>
          <input type="date" value="" class="ui-input " name="billDate" id="billDate" maxlength="10"
            style="width: 130px;">
        </li>
        <li>
          <label class="wt55"><span class="red">*</span>发票号:</label>
          <input type="text" id="billNo" class="ui-input">
        </li>
        <li>
          <label class="wt55">发票金额:</label>
          <input type="text" id="billAmount" class="ui-input" disabled="">
        </li>
        <li>
          <label class="wt55"><span class="red">*</span>发票抬头:</label>
          <!--<input type="text" id="billTitle" class="ui-input wt190 ">-->
          <div class="ctn-wrap"><span id=""><span class="ui-combo-wrap" style="width: 318px;"><input
                  type="text" id="billTitle" class="input-txt" autocomplete="off" style="width: 308px;"><span
                  class="aa"></span></span></span></div>
        </li>

      </ul>
    </div>

    <!--  以隐藏的方式获取商品的样式 ，作用是赋值 -->
    <div style="display:none;">
      <input type="text" id="supplierid" value="" autocomplete="off" readonly="">
    </div>

  </div>
</div>
<script type="text/javascript">
  function chooseSupplier() {

    layer.open({
			type: 2,
			title: "选择客户",
			shadeClose: true,
			shade: 0.3,
			maxmin: false, //开启最大化最小化按钮
			area: ['600px', '510px'],
			fixed: false, //不固定
			btn: ['确定', '取消'],
			content: "/admin/basic/choose_customer.html?single=0&is_customer=1",//多选，客户
			btn1: function (index, layero) {
				var body = layer.getChildFrame('body', index);
				//获取所有选中行
				var ids =  body.find("#grid").jqGrid('getGridParam','selarrrow');

				if (ids == '' || ids == null) {
					layer.msg("选择客户");
					return false;
				} else {
					//alert(ids);
					//return false;
					//获取商品信息
					$.get("/admin/basic/one_supplier.html", { ids: ids }, function (data, state) {
						if (data.status == 1) {
							$("#customer input").val(data.info.names);
							$('#supplierid').val(data.info.ids);
						} else {
							layer.msg("获取信息失败");
						}
					})
				}
				layer.close(index);
				return false;
			},
			btn4: function (index, layero) {
				layer.close(index);
				return false;
			}
		})

    
  }
  //鼠标移开时隐藏展开的列表
  $('#toolBottom').on('mouseleave.menuEvent', '.ui-btn-menu', function (e) {
    $(this).removeClass('ui-btn-menu-cur');
  });
  $('#toolBottom').on('mouseover', '.ui-btn-menu', function (e) {
    $(this).addClass('ui-btn-menu-cur');
  });
</script>

<script type="text/javascript">
  var Pdata;
  var edit_row = 0;
  var edit_col = 0;
  var row_id = 0;
  var col_name = 0;



  $(document).ready(function () {

    // 跳转打开新标签页
    $(".ta").click(function () {
      var tabid = $(this).attr('tabid');
      var tabTxt = $(this).attr('tabTxt');
      var _href = $(this).attr('_href');
      parent.tab.addTabItem({
        tabid: tabid,
        text: tabTxt,
        url: _href
      })
    });

    var outhtml = '';
    var inhtml = ' ';

    var startdate = $('#startdate').val();
    var enddate = $('#enddate').val();
    var supplierid = $('#supplierid').val();

    // 点击查询按钮
    $("#filter-submit").click(function () {
      
      $("div.grid-subtitle").text("日期: " + startdate + " 至 " + enddate);
      
      // 设置表格高度
      $(".ui-jqgrid .ui-jqgrid-bdiv").css({"max-height":$(window).height() - 240 - $("#filter-menu").height() + "px"});

      //此处可以添加对查询数据的合法验证 
      $("#grid").jqGrid('setGridParam', {
        datatype: 'json',
        postData: { 'ajax': '100', 'startdate': startdate, 'enddate': enddate, 'supplierid': supplierid}, //发送数据 
        page: 1
      }).trigger("reloadGrid"); //重新载入 
    });


    $("#grid").jqGrid({
      url: '/sales/salesbilling/add.html?ajax=100&startdate='+startdate+'&enddate='+enddate+'&supplierid='+supplierid,
      datatype: "json",//数据来源，本地数据（local，json,jsonp,xml等）
      height: '90%',//高度，表格高度。可为数值、百分比或'auto'
      colNames: ["id", "单据日期", "单据编号", "客户", "数量", "金额", "收款状态", "已开票金额", "未开票金额", "税额", "<span class='red'>*</span>本次开票金额"],
      colModel: [
        { name: 'id', index: 'id', width: 40, hidden: true },
        { name: 'dj_time', width: 100, label: "单据日期" },
        { name: 'sn', index: '', width: 120, label: "单据编号" },
        { name: 'supplier_name', index: '', width: 200, label: "客户", },
        { name: 'number', index: '', width: 100, label: "数量", editable: false ,align:'right'},
        { name: 'yh_price', index: '', width: 100, label: "金额", editable: false ,align:'right'},
        { name: 'payment_status', index: '', width: 80, label: "收款状态", editable: false },
        { name: 'y_invoice_price', index: '', width: 100, label: "已开票金额", editable: false ,align:'right'},
        { name: 'w_invoice_price', index: '', width: 100, label: "未开票金额", editable: false ,align:'right'},
        { name: 'tax_e', index: '', width: 100, label: "税额", editable: false,align:'right' },
        { name: 'invoice_price', index: '', width: 100, label: "本次开票金额", editable: true,align:'right' },
      ],
      cellEdit: true,
      cellurl: '/',
      rowNum: 100,
      rowList: [100, 200, 300],
      sortname: 'id',
      sortorder: "desc",
      pagerpos: "left",//分页位置

      pgbuttons: true,//翻页按钮
      pagination: true,
      altRows: !0,
      gridview: !0,
      shrinkToFit: !1,
      cellLayout: 8,
      autowidth: !0,
      pager: "#page",
      multiselect: true,//自带多选
      viewrecords: !0,
      cmTemplate: {
        sortable: !1,
        title: !1
      },
      // 合计
      footerrow: true,
      gridComplete: function () {
        // 设置表格高度
        $(".ui-jqgrid .ui-jqgrid-bdiv").css({"max-height":$(window).height() - 240 - $("#filter-menu").height() + "px"});

        var numbers = parseFloat($("#grid").getCol('number', false, 'sum')).toFixed(2);
        var yh_prices = parseFloat($("#grid").getCol('yh_price', false, 'sum')).toFixed(2);
        var y_invoice_prices = parseFloat($("#grid").getCol('y_invoice_price', false, 'sum')).toFixed(2);
        var w_invoice_prices = parseFloat($("#grid").getCol('w_invoice_price', false, 'sum')).toFixed(2);
        var tax_es = parseFloat($("#grid").getCol('tax_e', false, 'sum')).toFixed(2);
        var invoice_prices = parseFloat($("#grid").getCol('invoice_price', false, 'sum')).toFixed(2);
        $("#grid").footerData('set', { "dj_time": '合计：', number: numbers, yh_price:yh_prices ,y_invoice_price:y_invoice_prices ,w_invoice_price:w_invoice_prices ,tax_e:tax_es ,invoice_price:invoice_prices });
      },
      loadError: function (t, e, i) {
        parent.Public.tips({
          type: 1,
          content: "操作失败了哦，请检查您的网络链接！"
        })
      },
      beforeEditCell: function (rowid, cellname, value, iRow, iCol) {
        col_name = cellname;
        edit_row = iRow;
        edit_col = iCol;
        row_id = rowid;

      },

      beforeSelectRow: function (rowid, e) {
        var rowIds = $("#grid").jqGrid('getDataIDs');
        for (var k = 0; k < rowIds.length; k++) {
          var curRowData = $("#grid").jqGrid('getRowData', rowIds[k]);
          var curChk = $("#" + rowIds[k] + "").find(":checkbox");
          curChk.attr('name', 'checkboxname'); //给每一个checkbox赋名字
          curChk.attr('value', curRowData['invoice_price']); //给checkbox赋值
          //curChk.attr('checked', 'true'); //设置所有checkbox被选中
        }

        var rowid = jQuery("#grid").jqGrid("getGridParam", "selarrrow");
        //判断是否至少有一个选中
        var arr = [];
        if ($('.cbox').is(':checked')) {
          $("#grid input:checkbox[name='checkboxname']:checked").each(function (i) {
            arr.push($(this).val());
            //checkedTitles.push($(this).attr("title"));
          });
        }

        var money = 0;
        for (let index = 0; index < arr.length; index++) {
          const element = arr[index];
          money = money / 1 + element / 1;
        }
        $('#billAmount').val(money);
      },

      beforeSaveCell: function (rowid, cellname, value, iRow, iCol) {
        edit_row = iRow;
        edit_col = iCol;
        row_id = rowid;

        var numbers = parseFloat($("#grid").getCol('number', false, 'sum')).toFixed(2);
        var yh_prices = parseFloat($("#grid").getCol('yh_price', false, 'sum')).toFixed(2);
        var y_invoice_prices = parseFloat($("#grid").getCol('y_invoice_price', false, 'sum')).toFixed(2);
        var w_invoice_prices = parseFloat($("#grid").getCol('w_invoice_price', false, 'sum')).toFixed(2);
        var tax_es = parseFloat($("#grid").getCol('tax_e', false, 'sum')).toFixed(2);
        var invoice_prices = parseFloat($("#grid").getCol('invoice_price', false, 'sum')).toFixed(2);
        $("#grid").footerData('set', { "dj_time": '合计：', number: numbers, yh_price:yh_prices ,y_invoice_price:y_invoice_prices ,w_invoice_price:w_invoice_prices ,tax_e:tax_es ,invoice_price:invoice_prices });
      
      },
    });




    // 获取当前日期
    var myDate = new Date;
    var year = myDate.getFullYear(); //获取当前年
    var mon = myDate.getMonth() + 1; //获取当前月
    var date = myDate.getDate(); //获取当前日
    // console.log(year + '-' + mon + '-' + date)
    if (mon < 10) {
      mon = "0" + mon;
    }
    if (date < 10) {
      date = "0" + date;
    }
    $("#filter-fromDate,#filter-toDate,#billDate").val(year + '-' + mon + '-' + date);




  });

  function savedata() {
    var movedata = selectdata();
    var addtime = $("#billDate").val();
    var invoice_price = $("#billAmount").val();
    var invoice_no = $("#billNo").val();
    var invoice_title = $('#billTitle').val();

    $.ajax({
      type: 'POST',
      url: "save.html",
      data: { addtime: addtime, invoice_price: invoice_price, invoice_no: invoice_no, invoice_title: invoice_title, movedata: movedata },
      dataType: "json",
      async: false,
      error: function (request) {
        layer.msg("发送请求失败！");
      },
      success: function (data) {
        if (data.status == 1) {
          layer.msg(data.msg);
        } else {
          layer.msg(data.msg);
        }

      }
    });

  }

  //获取表格选中数据
  function selectdata() {
    var rowid = $("#grid").jqGrid("getGridParam", "selarrrow");
    var arr = new Array();
    for (let index = 0; index < rowid.length; index++) {
      var element = rowid[index];
      var rowData = jQuery("#grid").jqGrid("getRowData", element);
      arr[index] = rowData;
    }
    return arr;
  }

  function getJQAllData() {
    //拿到grid对象
    var obj = $("#grid");
    //获取grid表中所有的rowid值
    var rowIds = obj.getDataIDs();
    //初始化一个数组arrayData容器，用来存放rowData
    var arrayData = new Array();

    if (rowIds.length > 0) {
      for (var i = 0; i < rowIds.length; i++) {
        var arr = [];
        if ($('.cbox').is(':checked')) {
          $("#grid input:checkbox[name='checkboxname']:checked").each(function (i) {
            arr.push($(this).val());
            //checkedTitles.push($(this).attr("title"));
          });
        }

        var money = 0;
        for (let index = 0; index < arr.length; index++) {
          const element = arr[index];
          money = money / 1 + element / 1;
        }
        $('#billAmount').val(money);

        // if (Object.values(obj.getRowData(rowIds[i]))[2] != '') {
        //   arrayData.push(obj.getRowData(rowIds[i]));
        // }

        //rowData=obj.getRowData(rowid);//这里rowid=rowIds[i];
        // console.log(obj.getRowData(rowIds[i].goodsname));
        // return false;
        // if (obj.getRowData(rowIds[i].goodsname != '')) {
        //   arrayData.push(obj.getRowData(rowIds[i]));
        // }
      }
    }
    return arrayData;
  }

</script>

<script type="text/javascript">
  $(document).ready(function () {
    // 获取当前日期
    var myDate = new Date;
    var year = myDate.getFullYear(); //获取当前年
    var mon = myDate.getMonth() + 1; //获取当前月
    var date = myDate.getDate(); //获取当前日
    console.log(year + '-' + mon + '-' + date)
    if (mon < 10) {
      mon = "0" + mon;
    }
    if (date < 10) {
      date = "0" + date;
    }
    $("#startdate").val(year + '-' + mon + '-01');
    $("#enddate").val(year + '-' + mon + '-' + date);


  });

</script>
{include file="admin@public/footer" /}